/**
 * Created by LeungZ on 2016/10/17.
 */
import jsYaml from 'js-yaml';

class Custom {
    constructor($scope, commonSrv, ctgNotification, $timeout, $state,$stateParams) {
        "ngInject";
        $scope.$parent.vm.step =2;
        this._commonSrv=commonSrv;
        this.kDbCode=$stateParams.kDbCode;
        this.lessee_id=$stateParams.lesId;
        this._state = $state;
        this._notification = ctgNotification;
        this._fData = $scope.$parent.vm.fData;
        if(!this._fData.yJsonList){
            $state.go('k8s-lessee',{kDbCode:this.kDbCode});
            return;
        }
        else{

            this._fData.yJsonList.forEach((item,ky)=>{
                let yamlText = [];
                yamlText.push(jsYaml.safeDump(item));
                 $timeout(()=> {
                 this['editor'+ky].setValue(yamlText.join('\n---\n'));
                 },0,false);
            })
        }

         this.url='/ctg-imageconsole-web/namespace/create';this.action='创建';
        if(this.lessee_id){
            this.url='/ctg-imageconsole-web/namespace/update';
            this.action='修改';
        }
    }

    release() {
        if (this.editor0.options.lint.hasErr||this.editor1.options.lint.hasErr||this.editor2.options.lint.hasErr) return;

        this.loading = true;
        var item=this._fData.baseInfo;
        var params={
            id:parseInt(this.lessee_id,10),
            clusterCode:this.kDbCode,
            namespaceName:item.name,
            namespaceCode:item.code,
            cpuQuota:item.cpu,
            memoryQuota:item.ram,
            podQuota:item.living_count,
            // rcQuota:item.applied_count,
            namespaceYaml:this.editor0.getValue(),
            quotaYaml:this.editor1.getValue(),
            limitsYaml:this.editor2.getValue()
        };
       
         this._commonSrv('post',this.url,params).then((data)=>{
            this._notification.success(this.action+"成功.");
            this._state.go('k8s-lessee', {kDbCode: this.kDbCode});
         }).catch(()=> {
            this._notification.error(this.action+"失败.");
        }).finally(()=> {
            this.loading = false;
        });
       
    }
}
export default Custom